package com.dianjing.dianjing.util;


import com.dianjing.dianjing.enums.SysMessage;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ouwanfeng
 * @Date 2022/11/10 11:26
 */
public class ResponseData extends HashMap<String, Object> {

    /**
     * 序列化ID
     */
    private static final long serialVersionUID = 1L;

    /**
     * 默认构造函数
     */
    public ResponseData() {

        put("code", SysMessage.SUCCESS.getCode());
        put("msg", SysMessage.SUCCESS.getMsg());
        put("data",null);
    }

    /**
     * 正常
     *
     * @return
     */
    public static ResponseData success() {
        return new ResponseData();
    }

    /**
     * 系统正常并返回数据
     *
     * @param map
     * @return
     */
    public static ResponseData success(final Map<String, Object> map) {
        ResponseData responseData = new ResponseData();

        responseData.putAll(map);
        return responseData;
    }

    /**
     * 系统正常并返回对应消息
     *
     * @param msg
     * @return
     */
    public static ResponseData success(final String msg) {
        ResponseData responseData = new ResponseData();

        responseData.put("msg", msg);
        return responseData;
    }

    public ResponseData data( final Object value) {
        super.put("data", value);
        // 当前实例
        return this;
    }

    /**
     * 设置响应数据体，可以同时设置多个
     */
    @Override
    public ResponseData put(final String key, final Object value) {
        super.put(key, value);
        // 当前实例
        return this;
    }

    /**
     * 未知的系统异常
     *
     * @return
     */
    public static ResponseData fail() {
        return failed(SysMessage.FAIL.getCode(), SysMessage.FAIL.getMsg());
    }

    /**
     * 系统异常
     *
     * @param msg
     * @return
     */
    public static ResponseData fail(final String msg) {

        return failed(SysMessage.FAIL.getCode(), msg);
    }

    /**
     * 系统异常
     *
     * @param code
     * @param msg
     * @return
     */
    public static ResponseData failed(final int code, final String msg) {
        ResponseData responseData = new ResponseData();

        responseData.put("code", code);
        responseData.put("msg", msg);
        return responseData;
    }

}
